// pages/mine/apply_record/apply_record.js
import {
  Toast
} from 'wuss-weapp';
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    result: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: app.globalData.appTitle,
    })
    this.setData({
      theme: app.globalData.theme
    });
    app.setNavigationBarColor();
    // wx.getSystemInfo({
    //   success: res => {
    //     this.setData({
    //       height: res.windowHeight
    //     });
    //   }
    // });
    this.opinionBoxLoad();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    app.globalData.pageTotalNumber = 0;
    app.globalData.pageNumber = 1;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    app.globalData.pageNumber = 1;
    this.setData({
      result: []
    });
    this.opinionBoxLoad();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    app.globalData.pageNumber++;
    this.setData({
      mpLoadingShow: true,
      mpLoadingTips: "加载中..."
    });
    if (app.globalData.pageNumber <= app.globalData.pageTotalNumber) {
      this.opinionBoxLoad();
    } else {
      setTimeout(() => {
        this.setData({
          mpLoadingTips: "暂无数据"
        });
      }, 500);
      setTimeout(() => {
        this.setData({
          mpLoadingShow: false
        });
      }, 1000);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 加载数据
   */
  opinionBoxLoad: function () {
    app.wxGetRequest("/mine/getSuggestionList", {
      user_id: app.globalData.userInfo["user_id"],
      page_number: app.globalData.pageNumber,
      page_size: app.globalData.pageSize,
    }).then(result => {
      let data = result.data.data;
      let pageTotalNumber = data.list;
      this.setData({
        result: this.data.result.concat(data.list),
        noData: pageTotalNumber == 0 ? true : false,
        mpLoadingShow: false
      });
      app.globalData.pageTotalNumber = pageTotalNumber;
    });
  },
  /**
   * 新增
   */
  addOpinionBox: function () {
    wx.navigateTo({
      url: '/pages/mine/opinion_box/add_opinion_box/add_opinion_box',
    })
  },
  /**
   * 提交
   */
  formSubmit: function (e) {
    let params = e.detail;
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    if (!app.removeAllSpace(params.problem)) {
      this.setData({
        error: "请输入留言内容!"
      });
    } else {
      app.confirm('提示', '你确定要上报意见反馈吗？').then(result => {
        wx.showLoading({
          title: '保存中...',
        })
        app.wxPostRequest("/mine/submitSuggestion", {
          "InfoComplaintAdvice.area_code": app.globalData.parentAreaCode,
          "InfoComplaintAdvice.problem": params.problem,
          "InfoComplaintAdvice.user_id": app.globalData.userInfo["user_id"],
        }).then(result => {
          wx.showToast({
            title: result.data.msg,
          });
          app.globalData.pageNumber = 1;
          this.setData({
            result: []
          });
          this.opinionBoxLoad();
        });
      });
    }
  }
})